<?php

namespace app\shop\controller;

use think\Controller;

class Comment extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->BusinessModel = model('Business.Business');
        // $this->AddressModel = model('Business.Address');
        // $this->OrderModel = model('Order.Order');
        $this->OrderProductModel = model('Order.Product');
        // $this->CartModel = model('Product.Cart');
        $this->CommentModel=model('Order.Comment');

        $this->business = [];
        if ($this->request->isPost()) {
            $busid = $this->request->param('busid', 0, 'trim');
            $this->business = $this->BusinessModel->find($busid);
            if (!$this->business) {
                $this->error('请先登录');
                exit;
            }
        }
    }

    public function index()
    {
        if ($this->request->isPost())
        {
            $orderid=$this->request->param('orderid',0,'trim');
            $proid=$this->request->param('proid',0,'trim');
            $where=[
                'orderid'=>$orderid,
                'proid'=>$proid
            ];
            $result=$this->OrderProductModel->with(['products'])->where($where)->find();
            if($result)
            {
                $this->success('返回订单商品数据',null,$result);
                exit;
                
            }else
            {
                $this->error('订单商品不存在');
                exit;
            }
        }
    }

    // 添加评论
    public function add()
    {
        if ($this->request->isPost()) 
        {
            $params=$this->request->param();
            $orderid=$this->request->param('orderid',0,'trim');
            $proid=$this->request->param('proid',0,'trim');

            $where=[
                'orderid'=>$orderid,
                'proid'=>$proid
            ];
            $orderproduct=$this->OrderProductModel->where($where)->find();
            if(!$orderproduct)
            {
                $this->error('订单商品不存在');
                exit;
            }
            // halt($orderproduct->toArray());
            // exit;
            // $result=$this->

            // 加载模型
            
            $OrderProductModel = model('Order.Product');
            $CommentModel=model('Order.Comment');

            // 开启事务
            $OrderProductModel->startTrans();
            $CommentModel->startTrans();

            $Commentstatus=$CommentModel->validate('common/Order/Comment')->save($params);
            if($Commentstatus===false)
            {
                $this->error($CommentModel->getError());
                exit;
            }

            // 更新订单商品表评论状态
            $OrderProductData=[
                'id'=>$orderproduct['id'],
                'commentstatus'=>'1',
            ];

            // 更新
            $OrderProductStatus=$OrderProductModel->isUpdate(true)->save($OrderProductData);
            if($OrderProductStatus===false)
            {
                $CommentModel->rollback();
                $this->error('更新订单商品评论状态失败');
                exit;
            }
            if($Commentstatus === false || $OrderProductStatus === false)
            {
                $OrderProductModel->rollback();
                $CommentModel->rollback();
                $this->error('评论失败');
                exit;
            }
            else
            {
                $CommentModel->commit();
                $OrderProductModel->commit();
                $this->success('评论成功');
                exit;
            }
            
        }
    }
    
    public function list()
    {
        if($this->request->isPost())
        {
            $proid=$this->request->param('proid',0,'trim');
            $action=$this->request->param('action','','trim');
            $page=$this->request->param('page',1,'trim');
            $limit=10;
            $start=($page-1)*$limit;
            $where=[
                'proid'=>$proid,
            ];

            if($action=='all')
            {
                $comment=$this->CommentModel->with(['business'])->where($where)->order('createtime','desc')->limit($start,$limit)->select();
                if(!$comment)
                {
                    $this->error('暂无数据');
                    exit;
                }else
                {
                    $this->success('返回数据成功',null,$comment);
                    exit;
                }
            }

            $comment=$this->CommentModel->with(['business'])->where($where)->order('createtime','desc')->find();
            if(!$comment)
            {
                $this->error('暂无数据');
                exit;
            }else
            {
                $this->success('返回数据成功',null,$comment);
                exit;
            }
        }
    }
}